//
//  MessageTableViewCell.swift
//  FeastBooking
//
//  Created by Imon on 2017/11/26.
//  Copyright © 2017年 Imon. All rights reserved.
//

import UIKit

class MessageTableViewCell: UITableViewCell {

    //var hasRead = false
    
    let title: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.black
        label.textAlignment = .left
        label.font = AppFont(size: 16)
        label.text = LanguageHelper.getString(key: "SYS_NOTIFICATION")
        return label
    }()
    
    let date: UILabel = {
        let label = UILabel()
        label.textColor = lightTextColor
        label.textAlignment = .left
        label.font = AppFont(size: 15)
        return label
    }()
    
    let content: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.gray
        label.textAlignment = .left
        label.font = msgContentFont
        label.numberOfLines = 0
        return label
    }()
    
    let more: UILabel = {
        let label = UILabel()
        label.textColor = proceededColor
//        label.textAlignment = .left
        label.font = AppFont(size: 14)
        label.text = "\(LanguageHelper.getString(key: "CHECK_DATAIL")) >"
        return label
    }()
    
    let saperator: UIView = {
        let view = UIView()
        view.backgroundColor = saperatorColor
        return view
    }()
    
    let gap: UIView = {
        let view = UIView()
        view.backgroundColor = lightSaperatorColor
        return view
    }()
    
    let redPoint: UIView = {
        let view = UIView()
        view.backgroundColor = redPointColor
        return view
    }()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        _init()
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        _init()
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        _init()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
    }

    func initValue(message: SystemMessage) {
        self.content.text = message.message
        self.date.text = message.received_time
        if message.has_read == 0 {
            contentView.addSubview(redPoint)
        } else {
            redPoint.removeFromSuperview()
        }
    }
    
    private func _init() {
        self.contentView.backgroundColor = UIColor.white
        contentView.addSubview(title)
        //contentView.addSubview(redPoint)
        contentView.addSubview(date)
        contentView.addSubview(content)
        contentView.addSubview(saperator)
        contentView.addSubview(more)
        contentView.addSubview(gap)
    }
    
    override func layoutSubviews() {
        let redPointSize: CGFloat = 8
        let textMaxSize = CGSize(width: contentView.bounds.width - 20, height: contentView.bounds.height)
        let titleSize = TextSize.textSize(text: title.text!, font: title.font, maxSize: textMaxSize)
        let dateSize = TextSize.textSize(text: date.text!, font: date.font, maxSize: textMaxSize)
        let contentSize = TextSize.textSize(text: content.text!, font: content.font, maxSize: textMaxSize)
        let moreSize = TextSize.textSize(text: more.text!, font: more.font, maxSize: textMaxSize)
        title.frame = CGRect(x: 10, y: 15, width: titleSize.width, height: titleSize.height)
        redPoint.frame = CGRect(x: title.frame.origin.x + title.frame.width - redPointSize / 2, y: title.frame.origin.y, width: redPointSize, height: redPointSize)
        redPoint.layer.cornerRadius = redPointSize / 2
        redPoint.layer.masksToBounds = true
        date.frame = CGRect(x: contentView.bounds.width - dateSize.width - 10, y: title.frame.origin.y, width: dateSize.width, height: dateSize.height)
        content.frame = CGRect(x: 10, y: title.frame.origin.y + title.frame.height + 15, width: contentSize.width, height: contentSize.height)
        saperator.frame = CGRect(x: 10, y: content.frame.origin.y + content.frame.height + 10, width: contentView.bounds.width - 20, height: 0.5)
        more.frame = CGRect(x: contentView.bounds.width - moreSize.width - 15, y: saperator.frame.origin.y + saperator.frame.height + 10, width: moreSize.width, height: moreSize.height)
        gap.frame = CGRect(x: 0, y: self.contentView.bounds.height - 8, width: self.contentView.bounds.width, height: 8)
    }
}
